﻿using System;
using OpenQA.Selenium;
using System.Configuration;
using OpenQA.Selenium.Firefox;
using NUnit.Framework;

namespace mvcForum.Tests.WebUI {

	[TestFixture]
	public class H500_TopicTests {
		private static IWebDriver driver;

		#region Initialization and cleanup
		[TestFixtureSetUp]
		public static void TestInitialize() {
			driver = TestHelper.GetDriver();

			if (!driver.LogOn(Constants.Users.Admin.Email, Constants.Users.Admin.Password)) {
				throw new ApplicationException("could not log on!");
			}
		}

		[TestFixtureTearDown]
		public static void TestCleanup() {
			if (driver != null) {
				driver.Close();
			}
		}
		#endregion

		[Test]
		public void TestWriteTopic() {
			driver.VisitURL("forum");
			//driver.Navigate().GoToUrl(String.Format("{0}forum", TestHelper.SiteBaseURL));

			driver.GoToForum("Forum 1");

			//var link = driver.FindElement(By.LinkText("Forum 1"));
			//Assert.IsNotNull(link);
			//link.Click();
			//driver.Wait(1);

			Assert.AreEqual(String.Format("{0}forum/viewforum/forum-1/1", TestConfiguration.SiteBaseURL), driver.Url.ToLower());

			var link = driver.FindElement(By.LinkText("Post a new topic"));
			Assert.IsNotNull(link);
			link.Click();

			String topic = "My first topic!";

			IWebElement subjectBox = driver.FindElement(By.Name("Subject"));
			Assert.IsNotNull(subjectBox);
			subjectBox.SendKeys(topic);

			String body = "The content of my very first topic!";

			IWebElement bodyBox = driver.FindElement(By.Name("Body"));
			Assert.IsNotNull(bodyBox);
			bodyBox.SendKeys(body);

			IWebElement button = driver.FindElement(By.Name("post"));
			Assert.IsNotNull(button);
			button.Click();

			Assert.AreEqual(String.Format("{0}forum/viewforum/forum-1/1", TestConfiguration.SiteBaseURL), driver.Url.ToLower());

			Assert.AreEqual(true, driver.PageSource.Contains(topic));
			// TODO: Find topic!!
			// TODO: Find username and time of post!!
		}
	}
}